package de.Hijack.Giuliani;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import java.util.ArrayList;

public class MapOverlay extends Overlay {

    /**
     * android log TAG
     */
    private static final String TAG = "MapOverlay";
    private MainMapActivity context;
    private int activeFloor = 0;
    private ArrayList<Floor> floors = new ArrayList<Floor>();
    private LinearLayout buttons;

    public MapOverlay(MainMapActivity c, LinearLayout buttons) {
        super();
        this.context = c;
        this.buttons = buttons;
        loadMaps();
        initButtons();
        changeFloor(1);
    }

    private void loadMaps() {
        Map ruwg = new Map("RUWG_00_.svg", 49000074, 12092600, 48998617, 12095003, context);
        Map vgEg = new Map("VG_EG_.svg", 49000599, 12094524, 49000260, 12095318, context);
        Map zhU1 = new Map("ZHGB_D_U1_v1.1_basic.svg", 48999454, 12094231, 48998444, 12095920, context);
        Map zh00 = new Map("ZHGB_D_00_svg_basic.svg", 48999314, 12094231, 48998259, 12095422, context);
        Map ruwgu = new Map("RUWG_D_U1.svg",49000084,12092578,48998606,12095094,context);
        Floor nullFloor = new Floor("0");
        Floor groundFloor = new Floor("1");
        Floor firstFloor = new Floor("2");
        nullFloor.addMap(ruwgu);
        groundFloor.addMap(zhU1);
        firstFloor.addMap(vgEg);
        firstFloor.addMap(ruwg);
        firstFloor.addMap(zh00);
        floors.add(nullFloor);
        floors.add(groundFloor);
        floors.add(firstFloor);
    }

    /**
     * changes the floor to a given id or doing nothing if that floor doesnt exist
     * @param floor 
     */
    public void changeFloor(int floor){
        //change the floor and handle the buttons
        if(floors.size()>floor){
            activeFloor=floor;
            for(int i=0;i<buttons.getChildCount();i++){
                if(i==floor){
                    buttons.getChildAt(i).setEnabled(false);
                }else{
                    buttons.getChildAt(i).setEnabled(true);
                }
            }
        }
        context.update();
    }
    
    /**
     * returns the active floor (id)
     * @return the id of the active floor
     */
    public int getFloor(){
        return activeFloor;
    }
    
    private void initButtons(){
        for(int i=0;i<floors.size();i++){
            Button floorButton = new Button(context);
            floorButton.setText(floors.get(i).toString());
            final int floor = i;
            floorButton.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    changeFloor(floor);
                }
            });
            buttons.addView(floorButton);
        }
        
    }
    
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, shadow);
        // automatically set the first floor if none is set yet
        if (floors.get(activeFloor) == null){
            activeFloor = 0;
            return;
        }
        if (context.mode == MainMapActivity.MODE_REDPIN) {
            Paint paint = new Paint();
            paint.setARGB(100, 0, 0, 0);
            canvas.drawRect(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()), paint);
            //Draw Maps 
            for (Map m : floors.get(activeFloor).getMaps()) {
                Point upperLeft = new Point();
                Point lowerRight = new Point();
                mapView.getProjection().toPixels(m.getUpperLeftCorner(), upperLeft);
                mapView.getProjection().toPixels(m.getLowerRightCorner(), lowerRight);

                canvas.drawPicture(m.getPicture(), new Rect(upperLeft.x, upperLeft.y, lowerRight.x, lowerRight.y));

            }
        }
    }

    /**
     * a floor to be able to show different indoor maps on different floors
     */
    private class Floor {
        // Maps

        private ArrayList<Map> maps = new ArrayList<Map>();
        private String name;

        public Floor(String name) {
            this.name = name;
        }

        public void addMap(Map m) {
            maps.add(m);
        }

        public ArrayList<Map> getMaps() {
            return maps;
        }

        @Override
        public String toString() {
            return name;
        }
    }
}